package com.siecomzg.transport;

import android.os.Bundle;

import com.siecomzg.proto.Message;
import com.siecomzg.tool.LogUtil;

import com.siecomzg.constconfig.ConstDefine;
import com.siecomzg.constconfig.ErrorDefine;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Created on 2017/1/19.
 */

public class CallbackMap {

    private static final String TAG = "CallbackMap";
    private static volatile HashMap<String, CallbackTask> callbackTaskHashMap = new HashMap<String, CallbackTask>();
    private static  volatile HashMap<Integer, Message.siecomproto> taskTypeMap = new HashMap<Integer, Message.siecomproto>();
    private Message.siecomproto currentMsg = null;

    public static void clearMap(){

        taskTypeMap.clear();
        callbackTaskHashMap.clear();

    }

    public Message.siecomproto getCurrentMsg() {

        if (currentMsg!=null&&callbackTaskHashMap.containsKey(currentMsg.getMsgid())) {

            return currentMsg;

        }else{
            return null;
        }

    }

    private static CallbackMap ourInstance = new CallbackMap();

    public static CallbackMap getInstance() {
        return ourInstance;
    }

    Timer timer = null;
    TimerTask task = null;

    public void addCallbackTask(final Message.siecomproto inMsg, final DeviceCallback callback, int timeOutMs, final boolean isOnMain) {

            currentMsg = inMsg;
            //先获取相同模块的最后一个任务取消掉定时器
            if (taskTypeMap.containsKey(inMsg.getModule())) {
                Message.siecomproto msg = taskTypeMap.get(inMsg.getModule());
                CallbackTask currentTask = callbackTaskHashMap.get(msg.getMsgid());
                if(currentTask!=null) {
                    currentTask.getTimer().cancel();
                    LogUtil.e(TAG, "currentTask.getTimer().cancel");
                    taskTypeMap.remove(inMsg.getModule());
                }
                LogUtil.e(TAG, "taskTypeMap size---" + taskTypeMap.size() + "---");

            }


            final int requestCmd = inMsg.getCmd();

            CallbackTask callbackTask = new CallbackTask();
            callbackTask.setCallback(callback);
            callbackTask.setOnMain(isOnMain);
             timer = new Timer();
             task = new TimerTask() {
                @Override
                public void run() {

                    if (callbackTaskHashMap.containsKey(inMsg)) {
                        callbackTaskHashMap.remove(inMsg);
                    }

                    Bundle bundle = new Bundle();

                    switch (requestCmd){
                        case ConstDefine.FINGERFEARURE_REQUEST:
                            bundle.putInt("code", ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.FINGERPRINT_FREATURE_TIMEOUT));
                            break;
                        case ConstDefine.FINGER_VER_REQUEST:
                            bundle.putInt("code", ErrorDefine.FINGERPRINT_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.FINGERPRINT_TIMEOUT));
                            break;
                        case ConstDefine.ID_READ_REQUEST:
                            bundle.putInt("code", ErrorDefine.ID_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.ID_TIMEOUT));
                            break;
                        case ConstDefine.IC_REQUEST:
                            bundle.putInt("code", ErrorDefine.IC_CRAD_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.IC_CRAD_TIMEOUT));
                            break;
                        case ConstDefine.PIC_REQUEST:
                            bundle.putInt("code", ErrorDefine.PIC_CRAD_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.PIC_CRAD_TIMEOUT));
                            break;
                        case ConstDefine.MSR_REQUEST:
                            bundle.putInt("code", ErrorDefine.MSR_CARD_TIME);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.MSR_CARD_TIME));
                            break;
                        case ConstDefine.AUTO_REQUEST:
                            bundle.putInt("code", ErrorDefine.AUTO_CRAD_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.AUTO_CRAD_TIMEOUT));
                            break;
                        case ConstDefine.ARQC_REQUEST:
                            bundle.putInt("code", ErrorDefine.ARPC_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.ARPC_TIMEOUT));
                            break;
                        case ConstDefine.IC_LOG_REQUEST:
                            bundle.putInt("code", ErrorDefine.IC_LOG_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.IC_LOG_TIMEOUT));
                            break;
                        case ConstDefine.PIC_LOG_REQUEST:
                            bundle.putInt("code", ErrorDefine.PIC_LOG_TIMEOUT);
                            bundle.putString("message",ErrorDefine.getErrorDescribe(ErrorDefine.PIC_LOG_TIMEOUT));
                            break;
                        case ConstDefine.INPUT_MAIN_REQUEST:
                            bundle.putInt("code", ErrorDefine.INPUT_MAINKEY_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.INPUT_MAINKEY_TIMEOUT));
                            break;
                        case ConstDefine.INPUT_WORK_REQUEST:
                            bundle.putInt("code", ErrorDefine.INPUT_WORK_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.INPUT_WORK_TIMEOUT));
                            break;
                        case ConstDefine.INPUT_KEY_REQUEST:
                            bundle.putInt("code", ErrorDefine.KEY_TIME_OUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.KEY_TIME_OUT));
                            break;
                        case ConstDefine.SET_ENCRYPT_REQUEST:
                            bundle.putInt("code", ErrorDefine.KEY_ENCRYPT_TIMEOUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.KEY_ENCRYPT_TIMEOUT));
                            break;
                        default:
                            bundle.putInt("code", ErrorDefine.TIME_OUT);
                            bundle.putString("message", ErrorDefine.getErrorDescribe(ErrorDefine.TIME_OUT));
                            break;
                    }


                    SiecomDevice.deliverResult(callback, SiecomDevice.Result.ERROR, bundle, isOnMain);
                    LogUtil.e("callbackMap size:", "==" + callbackTaskHashMap.size());

                }
            };
            timer.schedule(task, timeOutMs + 1000);
            callbackTask.setTimer(timer);
            callbackTaskHashMap.put(inMsg.getMsgid(), callbackTask);
            taskTypeMap.put(inMsg.getModule(),inMsg);

    }


    public static HashMap<String, CallbackTask> getCallbackTaskHashMap() {
        return callbackTaskHashMap;
    }

    public static void removeCallback(String msgid) {
        if (callbackTaskHashMap.containsKey(msgid)) {
            callbackTaskHashMap.remove(msgid);
        }
    }

}
